package com.tianyadu.security.controller;

import com.tianyadu.security.domain.User;
import com.tianyadu.security.domain.UserInfo;
import com.tianyadu.security.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @FileName: UserController
 * @Description:
 * @author: xueyj
 * @create: 2018-12-04 15:45
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController",description="用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    /** 社交登陆工具类 */
    @Autowired
    private ProviderSignInUtils providerSignInUtils;

    @PostMapping("/regist")
    public void regist(User user, HttpServletRequest request) {

        //不管是注册用户还是绑定用户，都会拿到一个用户唯一标识。
        String userId = user.getUsername();
        providerSignInUtils.doPostSignUp(userId, new ServletWebRequest(request));
    }

    @GetMapping("/me")
    public Object getCurrentUser(Authentication authentication) {
        return authentication;
    }

    @GetMapping("/userDetail")
    public Object getUserDetails(@AuthenticationPrincipal UserDetails userDetails) {
        return userDetails;
    }

    /**
     * @Description: 创建用户信息
     * @param:
     * @return:
     * @author:
     * @date:   2018/12/4 17:20
     */
    @PostMapping
    @ApiOperation(value = "创建用户")
    public String createUserInfo(@Valid @RequestBody UserInfo userInfo) {
        UserInfo user = userService.saveUserInfo(userInfo);
        return user.toString();
    }

    /**
      * @Description: 根据用户id删除用户信息
      * @param:
      * @return:
      * @author:
      * @date:   2018/12/5 14:45
      */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        userService.deleteUserById(id);
        System.out.println(id);
    }

    /**
      * @Description: 根据用户id修改用户信息
      *  putMapping("/{id:\\d+}")id为数字
      * @param:
      * @return:
      * @author:
      * @date:   2018/12/5 14:44
      */
    @PutMapping("/{id}")
    public UserInfo update(@Valid @RequestBody UserInfo userInfo, BindingResult errors) {
        UserInfo updateUserInfo = userService.updateUserInfo(userInfo);

        return updateUserInfo;
    }
    /**
      * @Description: 查询全部用户
      * @param:
      * @return:
      * @author:
      * @date:   2018/12/4 16:25
      */
    @GetMapping
    public List<UserInfo> queryUserList() {
        return userService.findAll();
    }

    @GetMapping("/{id}")
    public String queryUserById(@PathVariable String id) {
        UserInfo userInfo = userService.findById(id);
        return userInfo.toString();
    }
}